package model.inventory;
// Generated Oct 27, 2012 8:43:23 AM by Hibernate Tools 3.2.1.GA


import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.AttributeOverride;
import javax.persistence.AttributeOverrides;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OrderBy;
import javax.persistence.Table;
import javax.persistence.Transient;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.hibernate.annotations.Cascade;
import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;
import org.hibernate.annotations.LazyCollection;
import org.hibernate.annotations.LazyCollectionOption;
import utility.typeSafe.DiskonType;


/**
 * MstItemHpp generated by hbm2java
 */
@Entity
@Table(name="mst_item_hpp",schema="inventory")
@Cache(usage=CacheConcurrencyStrategy.READ_WRITE)
public class ItemHpp implements java.io.Serializable {

    private ItemHppId id;
    private Unit unit;
    private Item item;
    private Integer fraksi;
    private BigDecimal price  = BigDecimal.ZERO;
    private BigDecimal diskon = BigDecimal.ZERO;
    private BigDecimal hpp = BigDecimal.ZERO;
    private BigDecimal ppn = BigDecimal.ZERO;
    private BigDecimal hppAfterPpn = BigDecimal.ZERO;
    private BigDecimal avgPrice = BigDecimal.ZERO;
    private Integer top;
    private Boolean pkp;
    private String kondisiBarang;
    private List<ItemBonus> itemBonuses;
    private List<ItemDiskon> itemDiskons;
    private Boolean select;
    
    public ItemHpp() {
        this.id=new ItemHppId();
        this.unit=new Unit();
        this.item=new Item();
        this.itemDiskons = new ArrayList<>();
        this.itemBonuses = new ArrayList<>();
        this.itemBonuses.add(new ItemBonus());
        this.itemDiskons.add(new ItemDiskon(DiskonType.PERSENTASE.toString()));
        this.select = false;
    }
   
    @EmbeddedId
    @AttributeOverrides( {
        @AttributeOverride(name="itemId", column=@Column(name="item_id", nullable=false) ), 
        @AttributeOverride(name="startDate", column=@Column(name="start_date", nullable=false, length=13) ), 
        @AttributeOverride(name="supplierId", column=@Column(name="supplier_id", nullable=false) ) } )
    public ItemHppId getId() {
        return this.id;
    }
    
    public void setId(ItemHppId id) {
        this.id = id;
    }

    @ManyToOne(fetch=FetchType.LAZY)
    @JoinColumn(name="item_id", nullable=false, insertable=false, updatable=false)
    public Item getItem() {
        return item;
    }

    public void setItem(Item item) {
        this.item = item;
    }
    
    @ManyToOne(fetch=FetchType.LAZY)
    @JoinColumn(name="unit_id", nullable=false)
    public Unit getUnit() {
        return unit;
    }

    public void setUnit(Unit unit) {
        this.unit = unit;
    }
    
    @Column(name="fraksi")
    public Integer getFraksi() {
        return this.fraksi;
    }
    
    public void setFraksi(Integer fraksi) {
        this.fraksi = fraksi;
    }
    
    @Column(name="price", precision=12, scale=2)
    public BigDecimal getPrice() {
        return this.price;
    }
    
    public void setPrice(BigDecimal price) {
        this.price = price;
    }
    
    @Column(name="diskon", precision=12, scale=3)
    public BigDecimal getDiskon() {
        return this.diskon;
    }
    
    public void setDiskon(BigDecimal diskon) {
        this.diskon = diskon;
    }
    
    @Column(name="hpp", precision=12, scale=2)
    public BigDecimal getHpp() {
        return this.hpp;
    }
    
    public void setHpp(BigDecimal hpp) {
        this.hpp = hpp;
    }
    
    @Column(name="ppn", precision=12, scale=2)
    public BigDecimal getPpn() {
        return this.ppn;
    }
    
    public void setPpn(BigDecimal ppn) {
        this.ppn = ppn;
    }
    
    @Column(name="hpp_after_ppn", precision=12, scale=2)
    public BigDecimal getHppAfterPpn() {
        return this.hppAfterPpn;
    }
    
    public void setHppAfterPpn(BigDecimal hppAfterPpn) {
        this.hppAfterPpn = hppAfterPpn;
    }
    
    @Column(name="avg_price", precision=12, scale=2)
    public BigDecimal getAvgPrice() {
        return this.avgPrice;
    }
    
    public void setAvgPrice(BigDecimal avgPrice) {
        this.avgPrice = avgPrice;
    }
    
    @Column(name="top")
    public Integer getTop() {
        return this.top;
    }
    
    public void setTop(Integer top) {
        this.top = top;
    }
    
    @Column(name="kondisi_barang", length=1)
    public String getKondisiBarang() {
        return this.kondisiBarang;
    }

    @Column(name="pkp")
    public Boolean getPkp() {
        return pkp;
    }

    public void setPkp(Boolean pkp) {
        this.pkp = pkp;
    }
        
    public void setKondisiBarang(String kondisiBarang) {
        this.kondisiBarang = kondisiBarang;
    }
//    its work  
//    @OneToMany(cascade=CascadeType.ALL, fetch= FetchType.EAGER, mappedBy="itemHpp")
//    @Fetch(value = FetchMode.SUBSELECT)
    @OneToMany(cascade=CascadeType.ALL, mappedBy="itemHpp")
    @LazyCollection(LazyCollectionOption.FALSE)
    @Fetch(value = FetchMode.SUBSELECT)
    public List<ItemBonus> getItemBonuses() {
        return this.itemBonuses;
    }
    
    public void setItemBonuses(List<ItemBonus> mstItemBonuses) {
        this.itemBonuses = mstItemBonuses;
    }
    
    @OneToMany(cascade=CascadeType.ALL, fetch= FetchType.LAZY, mappedBy="itemHpp")
    @OrderBy("id.seq")
    @Cascade(org.hibernate.annotations.CascadeType.DELETE_ORPHAN)
    public List<ItemDiskon> getItemDiskons() {
        return this.itemDiskons;
    }
    
    public void setItemDiskons(List<ItemDiskon> mstItemDiskons) {
        this.itemDiskons = mstItemDiskons;
    }

    @Transient
    public Boolean isSelect() {
        return select;
    }

    public void setSelect(Boolean select) {
        this.select = select;
    }
    
    public static List<ItemDiskon> cloneDiskon(List<ItemDiskon> original) {
        List<ItemDiskon> copy = new ArrayList<>();
        for (int i=0; i<original.size();i++) {
            copy.add(ItemDiskon.clone(original.get(i)));
        }
        return copy;
    }
    
    public static List<ItemBonus> cloneBonus(List<ItemBonus> original) {
        List<ItemBonus> copy = new ArrayList<>();
        for (int i=0; i<original.size();i++) {
            copy.add(ItemBonus.clone(original.get(i)));
        }
        return copy;
    }
    
     public static ItemHpp clone(ItemHpp original) {
        ItemHpp copy = new ItemHpp();
        if (original != null) {
            copy.setId(new ItemHppId(original.getId().getItemId(), original.getId().getStartDate(), original.getId().getSupplierId()) );
            copy.setAvgPrice(original.getAvgPrice());
            copy.setDiskon(original.getDiskon());
            copy.setFraksi(original.getFraksi());
            copy.setHpp(original.getHpp());
            copy.setHppAfterPpn(original.getHppAfterPpn());
            copy.setItem(original.getItem());
            copy.setKondisiBarang(original.getKondisiBarang());
            copy.setPkp(original.getPkp());
            copy.setPpn(original.getPpn());
            copy.setPrice(original.getPrice());
            copy.setSelect(original.isSelect());
            copy.setTop(original.getTop());
            copy.setUnit(original.getUnit());
            copy.setItemDiskons(ItemHpp.cloneDiskon(original.getItemDiskons()));
            copy.setItemBonuses(ItemHpp.cloneBonus(original.getItemBonuses()));
        }
        return copy;
    }

}


